package cn.ainit.byf.wordbook;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.test.web.User;

@Controller
@RequestMapping("/word")
public class WordController {
	
	private WordManager wordManager;

	@Resource
	public void setWordManager(WordManager wordManager) {
		this.wordManager = wordManager;
	}
	
	@RequestMapping("/getWords")
	@ResponseBody
	public List<ByfBascodesetclass> getWords(String uid,HttpServletRequest request){
		List<ByfBascodesetclass> byfBascodesets = wordManager.getWords(uid);
		return byfBascodesets;
	}
	
	@RequestMapping("/getAllWord")
	@ResponseBody
	public List<ByfBascodeitem> getAllWord(String uid,HttpServletRequest request){
		List<ByfBascodeitem> list = wordManager.getAllItem(uid);
		return list;
	}

	@RequestMapping("/getWord")
	public String getWord(HttpServletRequest request){
		request.setAttribute("word", wordManager.getWord());
		return "/pages/wordbook/wordbook";
	}
	
	
	@RequestMapping("/addWord")
	public void addWord(ByfBascodeitem byfBascodeitem,HttpServletRequest request){
		wordManager.addWord(byfBascodeitem);
	}
	
	@RequestMapping("/delWord")
	@Transactional
	public String delWord(String roomIds,HttpServletResponse response)throws Exception{
		String result = "{\"result\":\"error\"}";
		if(wordManager.delWord(roomIds)){
			result = "{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/updateWord")
	public void updateUser(ByfBascodeitem item,HttpServletRequest request){
		wordManager.updateWord(item);
	}
}
